package com.fxsh168.server.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.entitys.BindStatus;
import com.fxsh168.server.entitys.PingJia;
import com.fxsh168.server.interfaces.Constant;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class BindKaActivity extends BaseActivity implements View.OnClickListener {


    private String mPhone;
    private String token;
    EditText edPutName, edPutKa, edPutAderss;
    Button btnBindYin;
    String mName, mNumber, mAdress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_ka);
        //获取手机号码和token
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");
        edPutName = (EditText) findViewById(R.id.edPutName);
        edPutKa = (EditText) findViewById(R.id.edPutNummber);
        edPutAderss = (EditText) findViewById(R.id.edPutAdress);
        btnBindYin = (Button) findViewById(R.id.btnBindYin);
        btnBindYin.setOnClickListener(this);
        getIsBinData();
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btnBindYin:
                mName = edPutName.getText().toString().trim();
                mNumber = edPutKa.getText().toString().trim();
                mAdress = edPutAderss.getText().toString().trim();
                if (canCahnge()) {
                    changeYin();
                }
                break;
        }
    }

    private boolean canCahnge() {
        if (TextUtils.isEmpty(mName)) {
            showToast("请输入姓名");
        } else if (TextUtils.isEmpty(mNumber)) {
            showToast("请输入银行卡号");
        } else if (mNumber.length() < 16 || mNumber.length() > 20) {
            showToast("请输入正确的银行卡号");
        } else if (TextUtils.isEmpty(mAdress)) {
            showToast("请输入开户行地址");
        } else {
            return true;
        }
        return false;
    }


    private void changeYin() {
        OkHttpUtils.post().url(Constant.BINDYINHANGKA).addParams("phone", mPhone).addParams("xingming", mName)
                .addParams("bank_account", mNumber).addParams("kaihuhang", mAdress).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("tag", "修改银行卡结果" + response);
                        PingJia regiYZMCGPD = new Gson().fromJson(response, PingJia.class);
                        String s = regiYZMCGPD.getCode();
                        if (s.equals("200")) {
                            showToast("修改成功");
                            finish();
                        } else {
                            showToast("修改失败");
                        }
                    }
                });
    }


    private void getIsBinData() {
        OkHttpUtils.post().addParams("phone", mPhone).addParams("token", token)
                .url(Constant.BINDACCOUNTGETDATAREQUESTCODE)
                .build().execute(new StringCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {
                showToast("连接不上服务器!");
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("tag", "获取绑定状态" + response);
                BindStatus bindStatus = new Gson().fromJson(response, BindStatus.class);
                String code = bindStatus.getCode();
                if (code.equals("200")) {
                    BindStatus.DataBean data = bindStatus.getData();
                    String bank_account = data.getBank_account();
                    String kaihuhang = data.getKaihuhang();
                    String xingming = data.getXingming();
                    if (!bank_account.equals("0")) {
                        edPutName.setText(xingming);
                        edPutKa.setText(bank_account);
                        edPutAderss.setText(kaihuhang);
                    }
                }


//                if (response != null) {
//                    GetBindAccountData getBindAccountData = new Gson().fromJson(response, GetBindAccountData.class);
//                    String code = getBindAccountData.getCode().trim();
//                    if (code.equals(200 + "")) {
//
//                    } else if (code.equals(201 + "")) {
//                        showToast("非法请求!");
//                    } else if (code.equals(202 + "")) {
//                        showToast("非法请求!");
//                    }
//                } else {
//                    showToast("数据为空!");
//                }
            }
        });
    }

}
